package com.itheima.demo03stream;

import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Demo04StreamAggregation {
    public static void main(String[] args) {
        Stream<Student> studentStream = Stream.of(
                new Student("赵丽颖", 58, 95),
                new Student("杨颖", 56, 88),
                new Student("迪丽热巴", 56, 99),
                new Student("柳岩", 52, 77));

        // 获取最大值
//        Optional<Student> max = studentStream.collect(Collectors.maxBy((s1, s2) -> s1.getScore() - s2.getScore()));
//        System.out.println("max = " + max.get());

        // 获取最小值
//        Optional<Student> min = studentStream.collect(Collectors.minBy((s1, s2) -> s1.getScore() - s2.getScore()));
//        System.out.println("min = " + min.get());

        // 求总和
//        Integer count = studentStream.collect(Collectors.summingInt(s -> s.getAge()));
//        System.out.println("count = " + count);

        // 求平均值
//        Double avg = studentStream.collect(Collectors.averagingInt(s -> s.getScore()));
//        System.out.println("avg = " + avg);

        // 统计数量
        Long count = studentStream.collect(Collectors.counting());
        System.out.println("count = " + count);
    }
}
